package com.giscloud.limp.service.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.CaseExecutionDao;
import com.giscloud.limp.dao.CaseFirstInstanceDao;
import com.giscloud.limp.entity.*;
import com.giscloud.limp.entity.dto.CaseExecutionDTO;
import com.giscloud.limp.service.CaseExecutionService;
import com.giscloud.limp.service.CaseWarnService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("caseExecutionService")
@Log4j2
public class CaseExecutionServiceImpl implements CaseExecutionService {
    private final  CaseExecutionDao caseExecutionDao;

    @Autowired
    private CaseFirstInstanceDao caseFirstInstanceDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;

    @Autowired
    private CaseWarnService caseWarnService;

    @Autowired
    public CaseExecutionServiceImpl(CaseExecutionDao caseExecutionDao) {
        this.caseExecutionDao = caseExecutionDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseExecutionDTO caseExecutionDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseExecutionDTO));
        PageHelper.startPage(caseExecutionDTO.getPage(), caseExecutionDTO.getRows());
        CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
        CaseExecutionExample.Criteria criteria = caseExecutionExample.createCriteria();
        List<CaseExecution> caseExecutions = caseExecutionDao.selectByExample(caseExecutionExample);
        return PageInfo.of(caseExecutions);
    }


    /**
     * 信息
     */
    @Override
    public CaseExecution getById(Integer caseExecutionId){
        log.debug("#getById parameter ={}", caseExecutionId);
        return caseExecutionDao.selectByPrimaryKey(caseExecutionId);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(CaseExecution caseExecution){
        log.debug("#save parameter ={}", JSON.toJSONString(caseExecution));
        caseExecution.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        if(caseExecutionDao.insertSelective(caseExecution) > 0 ){
            CaseWarn caseWarn = new CaseWarn();
            caseWarn.setCaseId(caseExecution.getCaseId());
            caseWarn.setLitigationStage(4);
            caseWarn.setWarnDate(caseExecution.getWarnDate());
            caseWarn.setWarnContent(caseExecution.getWarnContent());
            caseWarnService.save(caseWarn);
            CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseExecution.getCaseId());
            caseFirstInstance.setCaseId(caseExecution.getCaseId());
            caseFirstInstanceDao.updateByPrimaryKeySelective(caseFirstInstance);
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(CaseExecution caseExecution){
         log.debug("#updateById parameter ={}", JSON.toJSONString(caseExecution));
         caseExecution.setOperateStaffId(tokenUserUtil.getUserId().intValue());
         caseExecution.setVersion(new Date());
         CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
         CaseExecutionExample.Criteria criteria = caseExecutionExample.createCriteria();
         criteria.andCaseExecutionIdEqualTo(caseExecution.getCaseExecutionId());
         if(caseExecutionDao.updateByExampleSelective(caseExecution,caseExecutionExample) > 0){
             CaseWarn caseWarn = new CaseWarn();
             caseWarn.setCaseId(caseExecution.getCaseId());
             caseWarn.setLitigationStage(4);
             caseWarn.setWarnDate(caseExecution.getWarnDate());
             caseWarn.setWarnContent(caseExecution.getWarnContent());
             caseWarnService.updateById(caseWarn);
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> caseExecutionIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(caseExecutionIds));
        CaseExecutionExample caseExecutionExample = new CaseExecutionExample();
        CaseExecutionExample.Criteria criteria = caseExecutionExample.createCriteria();
        criteria.andCaseExecutionIdIn(caseExecutionIds);
        List<CaseExecution> caseExecutions = caseExecutionDao.selectByExample(caseExecutionExample);
        if(CollectionUtils.isNotEmpty(caseExecutions)){
            caseExecutionDao.deleteByExample(caseExecutionExample);
            for(CaseExecution caseExecution : caseExecutions){
                CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseExecution.getCaseId());
                caseFirstInstance.setCaseId(caseExecution.getCaseId());
                caseFirstInstanceDao.updateCaseClose(caseFirstInstance);
                CaseWarn caseWarn = new CaseWarn();
                caseWarn.setCaseId(caseExecution.getCaseId());
                caseWarn.setLitigationStage(4);
                caseWarnService.remove(caseWarn);
            }
        }
        return Result.success("删除成功");

    }

}